<?php
namespace app\index\controller;

use think\Db;
use think\Session;
use think\Request;

class Index
{
    /**前台主页 遍历
     * @return \think\response\View
     */
    public function index()
    {
        //登录处理
        $info = Session::get('user_info');

        $user_id = $info['user_id'];
        $art = Db::name('artcomment')->where('user_id', $user_id)->select();

        $posts = Db::name('post')->where('user_id', $user_id)->select();

        //精选文章
        $list = Db::name('article')->limit(9)->select();
        $data = array();
        foreach($list as $val)
        {
            $colname = Db::name('column')->field('colname')->where('column_id','=',$val['column_id'])->find();

            $val['colname'] = $colname;
            $data[] = $val;
        }

        //热门文章
        $love = Db::name('article')->limit(5)->order('artlove desc')->select();
        $datas = array();
        foreach($love as $v){
            $colname_hot = Db::name('column')->field('colname')->where('column_id',$v['column_id'])->find();
            $v['colname_hot'] = $colname_hot;
            $datas[] = $v;
        }

        $links = Db::name('links')->select();
        $column = Db::name('column')->select();

        return view('index/index',[
            'column'=>$column,
            'artlist'=>$data,
            'links'=>$links,
            'love'=>$datas,
            'user_id' => $info['user_id'],
            'username' => $info['username'],
            'sex' => $info['sex'],
            'age' => $info['age'],
            'userpic' => $info['userpic'],
            'tel' => $info['tel'],
            'email' => $info['email'],
            'integral' => $info['bp'],
            'list' => $art,
            'posts' => $posts
        ]);
    }

    //更新用户名
    public function update()
    {
        $info = Session::get('user_info');
        $username = $_POST['username'];
        $data = [
            'username' => $username
        ];
        $update = Db::name('index_user')->where('user_id', $info['user_id'])->update($data);
        $user_info = Db::name('index_user')->where('user_id', $info['user_id'])->find();

        if ($update > 0) {
            Session::delete('user_info');
            Session::set('user_info', $user_info);
        }
        return json($update);
    }

    //删除评论
    public function delete($id)
    {
        $del = Db::name('artcomment')->delete($id);
        if ($del > 0) {
            $info['status'] = true;
            $info['id'] = $id;
            $info['info'] = 'ID为: ' . $id . '的评论删除成功!';
        } else {
            $info['status'] = false;
            $info['id'] = $id;
            $info['info'] = 'ID为: ' . $id . '的评论删除失败,请重试!';
        }
        return json($info);
    }

    //删除post
    public function delpost($id)
    {
        $del = Db::name('post')->delete($id);
        if ($del > 0) {
            $info['status'] = true;
            $info['id'] = $id;
            $info['info'] = 'ID为: ' . $id . '的帖子删除成功!';
        } else {
            $info['status'] = false;
            $info['id'] = $id;
            $info['info'] = 'ID为: ' . $id . '的帖子删除失败,请重试!';
        }
        return json($info);
    }

    //电话邮箱换绑定
    public function edit(Request $request)
    {
        $info = Session::get('user_info');
        $req = $request->post();
        $data = [
            'tel' => $req['tel'],
            'email' => $req['email']
        ];
        $edit = Db::name('index_user')->where('user_id',$info['user_id'])->update($data);
        if ($data > 0){
            Session::delete('user_info');
            return redirect('login/email');
        }else{
            return redirect('/');
        }
    }

    //头像上传
    public function server()
    {
        $info = Session::get('user_info');
        $editname = md5(rand()).time().substr($_FILES['photo']['name'], strrpos($_FILES['photo']['name'], '.'));
        $data = [
            'userpic' => $editname
        ];
        $insert = Db::name('index_user')->where('user_id', $info['user_id'])->update($data);
        $user_info = Db::name('index_user')->where('user_id', $info['user_id'])->find();

        if(move_uploaded_file($_FILES['photo']['tmp_name'], './static/userhead/'. $editname)){
            $response['isSuccess'] = true;
            Session::delete('user_info');
            Session::set('user_info', $user_info);
            return redirect('/');
        }else{
            $response['isSuccess'] = false;
        }

        echo json_encode($response);
    }
}
